package com.ctg.itrdc.k8sconsole.control;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.K8sNodeDTO;
import com.ctg.itrdc.k8sconsole.service.IK8snodeService;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;

/**
 * 
 * @description: 宿主机 请求处理
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年8月23日 上午10:56:58 Piao.Wu v1.0.0 create
 *
 */
@Controller
@RequestMapping("/k8snode")
public class K8snodeController {

	@Resource
	private IK8snodeService k8snodeService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 
	 * @Description: 获取节点资源列表
	 * @author Piao.Wu 2016年8月23日 上午11:12:39
	 * @param perpage
	 * @param page
	 * @param nodeip
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryNodes", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryNodes(@RequestParam("clusterCode") String clusterCode,
			@RequestParam("perpage") String perpage, @RequestParam("page") String page,
			@RequestParam(value = "nodeip", required = false) String nodeip,
			@RequestParam(value = "groupId", required = false) String groupId) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = k8snodeService.getNodes(clusterCode, perpage, page, nodeip, groupId);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}

	/**
	 * 
	 * @Description: 根据ip串获取节点资源
	 * @author Piao.Wu 2016年8月23日 上午11:12:50
	 * @param ips
	 * @return CommonResult
	 *
	 */
	// TODO 无用接口，后续记得删除
	@RequestMapping(value = "/queryNodesByIps", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult queryNodesByIps(@RequestBody(required = false) K8sNodeDTO k8sNodeDTO) {

		List<Map<String, Object>> listMaps = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = k8snodeService.getNodesByIps(k8sNodeDTO.getClusterCode(), k8sNodeDTO.getIps());
				listMaps = JSONUtils.jsonToMapList(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}

		if (listMaps == null || listMaps.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, listMaps);
	}

	/**
	 * 
	 * @Description: 获取具体节点信息
	 * @author Piao.Wu 2016年8月23日 上午11:13:01
	 * @param nodeip
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryNodeDetail", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryNodeDetail(@RequestParam("clusterCode") String clusterCode,
			@RequestParam(value = "nodeip", required = false) String nodeip) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = k8snodeService.getNodeDetail(clusterCode, nodeip);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}

	/**
	 * 
	 * @Description: 获取具体节点动态资源
	 * @author Piao.Wu 2016年8月23日 上午11:13:11
	 * @param nodeip
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryNodeResource", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult queryNodeResource(@RequestBody(required = false) K8sNodeDTO k8sNodeDTO) {

		// Map<String, Object> map = null;
		List<Map<String, Object>> listMaps = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = k8snodeService.getNodeResource(k8sNodeDTO);
				listMaps = JSONUtils.jsonToMapList(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (listMaps == null || listMaps.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, listMaps);
	}

	/**
	 * 
	 * @Description: 获取集群node、pod、rc资源总数
	 * @author Piao.Wu 2016年8月23日 上午11:13:22
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/queryClusterResrTotal", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryClusterResrTotal(@RequestParam("clusterCode") String clusterCode) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = k8snodeService.getClusterResrTotal(clusterCode);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}

	/**
	 * 
	 * @Description: 节点启用或停用
	 * @author Piao.Wu 2016年8月25日 上午10:56:06
	 * @param nodeDto
	 * @return CommonResult
	 *
	 */
	@RequestMapping(value = "/startOrShutdownNode", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult startOrShutdownNode(@RequestBody K8sNodeDTO nodeDto) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = k8snodeService.startOrShutdownNode(nodeDto.getClusterCode(), nodeDto.getNodeip(),
						nodeDto.getUnschedulable());
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}

}
